package com.squirrel.michale;

import java.util.Arrays;

public class Day20250120_LeetCode003424 {

    public long minCost(int[] arr, int[] brr, long k) {
        // 只进行加减
        long onlyPlus = 0;
        for (int i = 0; i < arr.length; i++) {
            onlyPlus += Math.abs(arr[i] - brr[i]);
        }
        if (onlyPlus <= k) {
            return onlyPlus;
        }

        Arrays.sort(arr);
        Arrays.sort(brr);
        // 可以打乱
        long shuffer = k;
        for (int i = 0; i < arr.length; i++) {
            shuffer += Math.abs(arr[i] - brr[i]);
        }
        return Math.min(shuffer, onlyPlus);
    }
}
